﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class cp_EditThermostat : System.Web.UI.Page
{
    ThermostatMonitorLib.Thermostat thermostat;

    protected void Page_Load(object sender, EventArgs e)
    {
        int id = Convert.ToInt32(Request["id"]);
        if (id > 0)
        {
            thermostat = ThermostatMonitorLib.Thermostat.LoadThermostat(id);
            ThermostatMonitorLib.Location location = ThermostatMonitorLib.Location.LoadLocation(thermostat.LocationId);
            if (location.UserId != AppUser.Current.UserData.Id) Response.Redirect("/cp/login.aspx");
        }
        else
        {
            thermostat = new ThermostatMonitorLib.Thermostat();
            thermostat.ACSeer = 16;
            thermostat.ACTons = 3.5;
            thermostat.ACKilowatts = 2.625;
            thermostat.DisplayName = "New Thermostat";
            thermostat.FanKilowatts = 0.5;
            thermostat.IpAddress = "192.168.1.106";
        }
        if (!IsPostBack) Populate();
    }

    private void Populate()
    {
        LocationList.DataSource = ThermostatMonitorLib.Locations.LoadLocationsByUserId(AppUser.Current.UserData.Id);
        LocationList.DataBind();

        LocationList.SelectedValue = thermostat.LocationId.ToString();
        DisplayNameText.Text = thermostat.DisplayName;
        IpAddressText.Text = thermostat.IpAddress;
        TonsText.Text = thermostat.ACTons.ToString();
        SeerText.Text = thermostat.ACSeer.ToString();
        KilowattsLabel.Text = thermostat.ACKilowatts.ToString() + "kw";
        BrandList.SelectedValue = thermostat.Brand;
        FanKilowattsText.Text = thermostat.FanKilowatts.ToString();
    }

    private bool Validate()
    {
        List<string> errors = new List<string>();
        if (DisplayNameText.Text == "") errors.Add("Display name is required");
        if (IpAddressText.Text == "") errors.Add("IP address is required");
        double tons;
        int seer;
        if (!Double.TryParse(TonsText.Text, out tons)) errors.Add("Please enter a numeric number for A/C tons.");
        if (!Int32.TryParse(SeerText.Text, out seer)) errors.Add("Please enter a numeric number for A/C SEER rating.");
        double fanKilo;
        if (!Double.TryParse(FanKilowattsText.Text, out fanKilo)) errors.Add("Please enter a numeric number for fan kilowatts.  0.5 is average.");

        if (errors.Count == 0) ErrorLit.Text = ""; else ErrorLit.Text = "<div class=\"error\">" + String.Join(" ", errors.ToArray()) + "</div>";
        return errors.Count == 0;
    }

    protected void SaveButton_Click(object sender, EventArgs e)
    {
        if (Validate())
        {
            thermostat.LocationId = Convert.ToInt32(LocationList.SelectedValue);
            thermostat.DisplayName = DisplayNameText.Text;
            thermostat.IpAddress = IpAddressText.Text;
            thermostat.ACTons = Convert.ToDouble(TonsText.Text);
            thermostat.ACSeer = Convert.ToInt32(SeerText.Text);
            thermostat.ACKilowatts = (thermostat.ACTons * 12.0) / Convert.ToDouble(thermostat.ACSeer);
            thermostat.FanKilowatts = Convert.ToDouble(FanKilowattsText.Text);
            thermostat.Brand = BrandList.SelectedValue;

            ThermostatMonitorLib.Thermostat.SaveThermostat(thermostat);
            Response.Redirect("/cp/");
        }
    }
}
